package com.hiwan.system.service.menu;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hiwan.system.common.base.GenericService;
import com.hiwan.system.dao.menu.MenuDao;
import com.hiwan.system.pojo.MenuInfo;
import com.hiwan.system.pojo.TreeNode;
import com.hiwan.system.util.UserCacheUtils;

@Service
@Transactional
public class MenuService extends GenericService<MenuDao,MenuInfo,String>{
	

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private MenuDao menuDao;
	
	/**
	 * 根据父级菜单编号获取菜单列表
	 * @return
	 */
	public List<MenuInfo> getMenuInfoList(String parentId){
		List<MenuInfo> list = menuDao.selectMenuList(parentId);
		return list==null?Collections.<MenuInfo>emptyList():list;
	}
	
	/**
	 * 查询用户菜单列表
	 * 
	 * @param relationId
	 *            登陆用户的归属关系
	 * @return
	 */
	public List<MenuInfo> getMenuList(String relationId,String roleId) {
		List<String> menuIdList = menuDao.selectUserMenuList(relationId);
		if (menuIdList == null || menuIdList.isEmpty()) {
			return null;
		}

		List<MenuInfo> menus = new ArrayList<MenuInfo>();
		Map<String, MenuInfo> menuMap = new HashMap<String, MenuInfo>();
		for (MenuInfo menu : menuDao.selectMenusInfo(menuIdList,roleId)) {
			if ("1".equals(menu.getIsLeaf())) {
				menu.setMenuUrl("login/login!go.action?menuId="
						+ menu.getMenuId());
			}
			menuMap.put(menu.getMenuId(), menu);
			if ("0".equals(menu.getIsLeaf())) {
				menus.add(menu);
			} else {
				MenuInfo parent = menuMap.get(menu.getParentMenu());
				if (parent == null) {
					logger.error("未知的父节点菜单 : {}", menu.getParentMenu());
				} else {
					parent.addSubMenu(menu);
				}
			}
		}

		return menus;
	}

	/**
	 * 获取所有的菜单，供分配角色权限使用
	 * @version 2.0
	 * @return
	 
	public String getAllMenuList() {
		String menuMessage = "";
		List<MenuInfo> menuInfoList = (List<MenuInfo>) menuDao
				.selectAllMenusInfo(null);
		if (menuInfoList != null) {
			for (MenuInfo o : menuInfoList) {
				menuMessage += o.getMenuId() + "|" + o.getParentMenu() + "|"
						+ o.getMenuName() +"|"+hasChildMenus(o.getSubMenus()) +";";
			}
		}
		return menuMessage;
	}
	
	public int hasChildMenus(List<MenuInfo> subMenus){
		int sub = 0;
		if(subMenus != null && !subMenus.isEmpty()){
		for(MenuInfo m : subMenus){
			if(m.getMenuId() != null)
				sub++;
		  }
		 
		}
		return sub;
	}
	*/
	
	/**
	 *  @version 3.0
	 */
	public List<MenuInfo> getAllMenuInofList(String parentId) {
		List<MenuInfo> menuInfoList = (List<MenuInfo>) menuDao.selectAllMenusInfo(parentId);
		return menuInfoList;
	}
	/**
	 * 根据父菜单 得到子菜单的id集体
	 * @author lsb
	 * @since  2015-11-16 上午10:40:06
	 * @version hiwan 3.0	
	 * @param parentId
	 * @return
	 */
	public List<String > obtainMenuIdList(String parentId) {
		List<MenuInfo> menuInfoList = (List<MenuInfo>) menuDao.selectAllMenusInfo(parentId);
		List<String> list=new ArrayList<String>();
		for (MenuInfo menuInfo : menuInfoList) {
			if(menuInfo!=null){
				list.add(menuInfo.getMenuId());
			}
		}
		return list;
	}
	
	/**
	 * 获取菜单详情
	 * @param menuId
	 * @return
	 * @throws NoSuchFieldException 
	 */
	public MenuInfo getMenuInfo(String menuId){
		return menuDao.selectMenuInfo(menuId);
	}
	
	/**
	 * 更新菜单
	 * @param menuInfo
	 * @return
	 */
	public int modifyMenuInfo(MenuInfo menuInfo){
		return menuDao.updateMenuInfo(menuInfo);
	}
	
	/**
	 * 删除菜单
	 * @param menuInfo
	 * @return
	 */
	public int removeMenuInfo(String menuId){
		UserCacheUtils.removeCache(UserCacheUtils.CACHE_MENU_LIST);
		return menuDao.deleteMenuInfoByPK(menuId);
	}
	
	/**
	 * 新增菜单
	 * @param menuInfo
	 * @return
	 */
	public int addMenuInfo(MenuInfo menuInfo){
		return menuDao.insertMenuInfo(menuInfo);
	}
	
	
	public String checkSubMenu(String menuId){
		List<MenuInfo> list = this.getMenuInfoList(menuId);
		if(list == null || list.isEmpty()){
			return "success";
		}else{
			return "failure";
		}
	}
	/**
	 * 获取所有的菜单，供分配角色权限使用
	 * @version 
	 * @return
	 */
	public List<TreeNode> getAllMenuList() {
		List<TreeNode>  menuList = new ArrayList<TreeNode>();
		TreeNode treeNode =null;
		List<MenuInfo> menuInfoList = (List<MenuInfo>) menuDao.selectAllMenusInfo(null);
		if (menuInfoList != null) {
			for (MenuInfo o : menuInfoList) {
				treeNode=TreeNode.buildTreeNode(o.getMenuId(), o.getParentMenu(), o.getMenuName(), "",
						hasChildMenus(o.getSubMenus())+"", "", "", "", "", "false", "false");
				menuList.add(treeNode);
			}
		}
		return menuList;
	}
	public int hasChildMenus(List<MenuInfo> subMenus){
		int sub = 0;
		if(subMenus.size()>0){
		for(MenuInfo m : subMenus){
			if(m.getMenuId() != null)
				sub++;
		  }
		 
		}
		return sub;
	}
	
}